package cn.stylefeng.guns.modular.chain.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.stylefeng.guns.core.bipay.http.ResponseMessage;
import cn.stylefeng.guns.core.bipay.http.client.BiPayClient;
import cn.stylefeng.guns.core.exceptions.BusinessException;
import cn.stylefeng.guns.core.exceptions.UpdateDataException;
import cn.stylefeng.guns.modular.app.service.BiPayService;
import cn.stylefeng.guns.modular.app.vo.chain.WithdrawDto;
import cn.stylefeng.guns.modular.base.state.Constant;
import cn.stylefeng.guns.modular.base.state.ProConst;
import cn.stylefeng.guns.modular.base.state.F;
import cn.stylefeng.guns.modular.base.util.HttpUtils;
import cn.stylefeng.guns.modular.coin.entity.Spot;
import cn.stylefeng.guns.modular.coin.service.SpotService;
import cn.stylefeng.guns.modular.fin.entity.Currency;
import cn.stylefeng.guns.modular.fin.entity.Wallet;
import cn.stylefeng.guns.modular.fin.service.CurrencyService;
import cn.stylefeng.guns.modular.fin.service.WalletService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.stylefeng.guns.modular.chain.entity.Withdraw;
import cn.stylefeng.guns.modular.chain.mapper.WithdrawMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 提币信息Service
 *
 * @author yaying.liu
 * @Date 2019-12-10 20:05:21
 */
@Service
@Slf4j
public class WithdrawService extends ServiceImpl<WithdrawMapper, Withdraw>
{


    @Autowired
    WalletService walletService;


    @Autowired
    CurrencyService currencyService;

    @Value("${eth.key}")
    private String key;
    @Value("${eth.secret}")
    private String secret;
    @Value("${eth.memberCode}")
    private String memberCode;
    //创建钱包地址
    @Value("${eth.createUrl}")
    private String createUrl;
    //提幣地址
    @Value("${eth.mentionUrl}")
    private String mentionUrl;
    //地址校验
    @Value("${eth.checkAddressUrl}")
    private String checkAddressUrl;

    @Resource
    BiPayService biPayService;

    @Resource
    SpotService spotService;

    /**
     * 查询提币信息
     */
    public Page<Map<String, Object>> selectByCondition(Map condition)
    {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.selectByCondition(page, condition);
    }
    public List<Withdraw> selectByMemberId(Long... memberIds ){

        return this.baseMapper.selectByMemberIds(memberIds);
    };

    /**
     * 删除提币信息
     */
    public void deleteWithdraw(Long withdrawId)
    {
        Withdraw entity = this.baseMapper.selectById(withdrawId);
        //将删除标志设置为Y，表示删除
        entity.setDel("Y");
        this.baseMapper.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public ResponseData pass(Withdraw entity)
    {

            entity.setStatus(ProConst.WithdrawStatusEnum.PASS.getCode());
            this.updateById(entity);

        return ResponseData.success();
    }


    @Transactional(rollbackFor = Exception.class)
    public ResponseData fail(Withdraw entity)
    {

        Currency currency = F.me().getCurrency(entity.getMemberId(), entity.getType());

        entity.setStatus(ProConst.WithdrawStatusEnum.REJECT.getCode());

        Boolean ok =  this.updateById(entity);

       if(ok){
           ok = currencyService.operateCurrencyUsedPrice(currency.getMemberId(),currency.getType(),ProConst.CashFlowOpEnum.FLOW_IN,entity.getPrice(),currency.getUsedPrice(),"提币退回");
       }
       if(!ok){
           throw new BusinessException("提币处理失败");
       }

        return ResponseData.success();
    }


    /**
     * 添加提币信息
     */
    public void addWithdraw(Withdraw withdraw)
    {
        this.baseMapper.insert(withdraw);
    }
}
